package main

import (
	"flag"
	"github.com/zeromicro/go-zero/core/logx"
	"yunzhan/log-server/internal/config"
	"yunzhan/log-server/internal/handler"
	"yunzhan/log-server/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "etc/config.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c)

	logx.MustSetup(c.LogConf)
	defer func() {
		err := logx.Close()
		if err != nil {
			panic(err)
		}
	}()

	// 创建 REST 服务器
	serviceConfig, ok := c.RestServices["log-server"]
	if !ok {
		logx.Errorf("Service %s not found in config file", "log-server")
	}

	server := rest.MustNewServer(serviceConfig.RestConf)
	defer server.Stop()

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	logx.Infof("Starting %s at %s:%d...", serviceConfig.RestConf.Name, serviceConfig.RestConf.Host, serviceConfig.RestConf.Port)
	server.Start()
}
